<?php

require_once dirname(__FILE__).'/../lib/invoiceGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/invoiceGeneratorHelper.class.php';

/**
 * invoice actions.
 *
 * @package    billingSystem
 * @subpackage invoice
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class invoiceActions extends autoInvoiceActions
{

        protected function getPager()
        {

                $statues=  $this->getUser()->getAttribute('statues');
                $pager = $this->configuration->getPager('invoicedetails');
                if($statues==null or $statues=="")
                {
                        $pager->setQuery($this->buildQuery());
                }
                 else
                 {
                        $pager->setQuery($this->buildQuery()->addWhere('paystatus = ? ',  $statues));
                }
                $pager->setPage($this->getPage());
                $pager->init();
                return $pager;
        }
        function executeSetPaid()
        {
                 $this->getUser()->setAttribute('statues','done');
               $this->redirect('invoice');
        }
        function executeSetPendingDelvier()
        {
                 $this->getUser()->setAttribute('statues','none');
                                $this->redirect('invoice');

        }
        function executeSetPendingPayement()
        {
                 $this->getUser()->setAttribute('statues','partial');
                                $this->redirect('invoice');

        }
        function executeSetInvoiceAll()
        {
                 $this->getUser()->setAttribute('statues','');
                                $this->redirect('invoice');
        }

    function executeCreate(sfWebRequest $request)
        {
                $this->form=new InvoicedetailsForm();
                $this->processForm($request, $this->form) ;
        }
        function executeListAdjust(sfWebRequest $request)
        {
                $this->form=new InvoicedetailsForm();
                $this->invoice=$this->getRoute()->getObject();
                $this->totalmoney=$this->invoice->getTotmoney();
                $this->dueamount=$this->invoice->getDueamount();
                $this->paidamount=$this->totalmoney-$this->dueamount;
                $this->invid=  $this->invoice->getInvid();
                $this->inProducts=Doctrine_Core::getTable('Products')->getProducts($this->invid);
                $this->notinProducts=Doctrine_Core::getTable('Products')->getProducts($this->invid,true);
        }

        function executeListPayInvoice(sfWebRequest $request)
        {
                $this->form=new InvoicedetailsForm();
                //$this->invoice=$this->getRoute()->getObject();
                //$this->totalmoney=$this->invoice->getTotmoney();
                //$this->dueamount=$this->invoice->getDueamount();
                //$this->paidamount=$this->totalmoney-$this->dueamount;
                //$this->invid=  $this->invoice->getInvid();
                $this->inProducts=Doctrine_Core::getTable('Products')->getProducts($this->invid);
                $this->notinProducts=Doctrine_Core::getTable('Products')->getProducts($this->invid,true);
        }
        function processForm(sfWebRequest $request, sfForm $form)
        {
                $form->bind(
                        $request->getParameter($form->getName()),
                        $request->getFiles($form->getName())
                );

                if ($form->isValid())
                {
                        $invoicedetails = $form->save();
                        $invoice=$invoicedetails->getInvoice();
                        $invoice->setTotmoney($invoice->getTotmoney()-$invoicedetails->getTotalmoneyproduct());
                        $invoice->save();
                       //$this->redirect($this->generateUrl('add_details_invoice',$invoicedetails->getInvoice()));
                }
                else
                        echo "sssssssss".$form->getErrorSchema();
                        exit;
        }       
        public function executeSetSend(sfWebRequest $request)
        {
          $invoiceIds=explode(":",$request->getParameter('id'));
          foreach($invoiceIds as  $invId)
          {
            if($invId!=null)
            {
              $invArr=Doctrine_Core::getTable('Invoice')->searchById($invId);
              if(count($invArr)!=0)
              {
                $invoice=$invArr[0];
                $invoice->set("senddate",date('Y-m-d',time()));
                $invoice->set("paystatus","partial");
                $invoice->save();
              }
            }
          }
          $this->redirect('/invoice');

    }
}